package com.ruoyi.framework.manager;

import com.ruoyi.common.core.activiti.MyActivitiInterface;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>工作流管理类</p>
 * <p>创建日期：2021-07-19</p>
 *
 * @author zhou-qf
 */
@Component
public class MyActivitiManager {

    @Autowired(required = false)
    private List<MyActivitiInterface> myActivitiInterfaceList;

    /**
     * 通过候选用户获取人员姓名
     * @param users
     * @return
     */
    public List<SysUser> queryNameByUsers(String users, String processId, String processKey){
        if(myActivitiInterfaceList.isEmpty()){
            return null;
        }
        AtomicReference<List<SysUser>> sysUser = new AtomicReference();

        myActivitiInterfaceList.stream().forEach(item->{
            if(item.support(processKey)){
                sysUser.set(item.queryNameByUsers(users, processId));
            }
        });

        return sysUser.get();
    }

    /**
     * 通过候选组获取人员姓名
     * @param groups
     * @return
     */
    public List<SysUser> queryNameByGroups(String groups, String processId, String processKey){
        if(myActivitiInterfaceList.isEmpty()){
            return null;
        }

        AtomicReference<List<SysUser>> sysUser = new AtomicReference();

        myActivitiInterfaceList.stream().forEach(item->{
            if(item.support(processKey)){
                sysUser.set(item.queryNameByGroups(groups, processId));
            }
        });
        return sysUser.get();
    }

    /**
     * 保存轨迹信息
     * @param object
     * @param processKey
     */
    public void doSaveTrajectory(Object object, String processKey){
        if(myActivitiInterfaceList.isEmpty()){
            return;
        }

        myActivitiInterfaceList.stream().forEach(item->{
            if(item.support(processKey)){
                item.saveTrajectory(object);
            }
        });
    }
}
